<?php

class Pldkp_View_Helper_AutoSelector extends ZendX_JQuery_View_Helper_UiWidget
{
    public function autoSelector($id, $value = null, array $params = array(), array $attribs = array())
    {
        $attribs = $this->_prepareAttributes($id, $value, $attribs);
        
        $url = null;
        $shownColumn = null;
        $valueColumn = null;
        $shownValue = '';
        
        if (isset($params['url'])) {
            $url = "'" . $params['url'] . "'";
            unset($params['url']);
        } else {
            throw Exception('No url given!');
        }
        
        if (isset($params['shownColumn'])) {
            $shownColumn = $params['shownColumn'];
            unset($params['shownColumn']);
        } else {
            throw Exception('shownColumn must be assigned to autoselector!');
        }
        
        if (isset($params['valueColumn'])) {
            $valueColumn = $params['valueColumn'];
            unset($params['valueColumn']);
        } else {
            throw Exception('valueColumn must be assigned to autoselector!');
        }
        
        if (isset($params['shownValue'])) {
            $shownValue = $params['shownValue'];
            unset($params['shownValue']);
        }
         
        // Ensure that the proper javascript and css is loaded
        $baseUrl = $this->view->baseUrl();
        $this->jquery->addJavascriptFile($baseUrl . 'js/jquery/autocomplete/jquery.autocomplete.min.js');
        $this->jquery->addStylesheet($baseUrl . 'js/jquery/autocomplete/jquery.autocomplete.css');
        
        // Setup attribs for the hidden field and the shown field
        $displayAttribs = $attribs;
        $displayAttribs['id'] = 'autocomplete_' . $displayAttribs['id'];
        $hiddenAtribbs = $attribs;
        
        // http://view.jquery.com/trunk/plugins/autocomplete/demo/json.html
        // Setup the parameters to accept json and format it according to the table columns
        $params['dataType'] = 'json';
        
        $params['parse'] = new Zend_Json_Expr('
        function(data) {
            return $.pldkp.responseCallback(data, {
                onSucces: function(data) {
                    return $.map(data, function(row) {
                        return { 
                            data: row, 
                            value: row.' . $shownColumn . ', 
                            result: row.' . $shownColumn . ' 
                        };
                    });
                }
            });
        }');
        
        $params['formatItem'] = new Zend_Json_Expr('
        function(item) { 
            return item.' . $shownColumn . '; 
        }');
        
        $params = Zend_Json::encode($params, false, array('enableJsonExprFinder' => true));
        
        $jqHandle = ZendX_JQuery_View_Helper_JQuery::getJQueryHandler();
        
        $autocomplete = sprintf('%s("#%s").autocomplete(%s, %s);', $jqHandle, $displayAttribs['id'], $url, $params);
        $this->jquery->addOnLoad($autocomplete);
        $result = sprintf('%s("#%s").result(function(event, data, formatted) { if (data) %s("#%s").val(data.%s); });', $jqHandle, $displayAttribs['id'], $jqHandle, $hiddenAtribbs['id'], $valueColumn);
        $this->jquery->addOnLoad($result);
        
        $content  = $this->view->formText($displayAttribs['id'], $shownValue, $displayAttribs);
        $content .= $this->view->formHidden($hiddenAtribbs['id'], $value, $hiddenAtribbs);
         
        return $content;
    }
}